package com.tiennt.android.callnotes;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.tiennt.android.callnotes.db.DbAdapter;
import com.tiennt.android.callnotes.models.SwipeDismissListViewTouchListener;
import com.tiennt.android.callnotes.models.contentholders.ContactInfo;
import com.tiennt.android.callnotes.models.contentholders.SingleNote;
import com.tiennt.android.callnotes.models.listadapters.ListPopupNotesAdapter;
import com.tiennt.android.callnotes.utils.ContactUtils;
import com.tiennt.android.callnotes.utils.ProjectUtils;

public class PopupNotesActivity extends Activity {
	private final String TAG = "PopupNotesActivity";
	private final boolean DBG = true;

	private String contactId;
	private int type;

	private ListView mListNotes;
	private Button mBtnClose;
	private Button mBtnCloseAndDismiss;

	private ImageView imgvContactImage;
	private TextView tvContactName;

	private View.OnClickListener onBtnCloseClick;
	private View.OnClickListener onBtnCloseAndDismissClick;
	ListPopupNotesAdapter mListAdapter;
	DbAdapter mDbAdapter = new DbAdapter(PopupNotesActivity.this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
		getWindow().addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		getWindow().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_popup_notes);

		initialComponents();

	}

	private void initialComponents() {
		Bundle data = getIntent().getExtras();
		contactId = data.getString(ProjectUtils.BUNDLEKEY_CONTACTID);
		type = data.getInt(ProjectUtils.BUNDLEKEY_CALLTYPE);

		log("TYPE: " + type);
		log("ID: " + contactId);

		ContactInfo info = ContactUtils.getContactInfo(this,
				Long.parseLong(contactId));
		imgvContactImage = (ImageView) findViewById(R.id.imgvContactImage);
		imgvContactImage.setImageBitmap(info.getContactPhoto());

		tvContactName = (TextView) findViewById(R.id.tvContactName);
		tvContactName.setText(info.getContactName());

		mBtnClose = (Button) findViewById(R.id.btnClose);

		onBtnCloseClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				PopupNotesActivity.this.finish();
			}
		};

		mBtnClose.setOnClickListener(onBtnCloseClick);

		mBtnCloseAndDismiss = (Button) findViewById(R.id.btnDismissAndClose);

		onBtnCloseAndDismissClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				doDismissAllNotes();
				PopupNotesActivity.this.finish();
			}
		};

		mBtnCloseAndDismiss.setOnClickListener(onBtnCloseAndDismissClick);

		mListNotes = (ListView) findViewById(R.id.lvNotes);

		mListAdapter = new ListPopupNotesAdapter(this, R.layout.popup_list_row,
				getAllNotes());
		mListNotes.setAdapter(mListAdapter);

		SwipeDismissListViewTouchListener touchListener = new SwipeDismissListViewTouchListener(
				mListNotes,
				new SwipeDismissListViewTouchListener.OnDismissCallback() {
					@Override
					public void onDismiss(ListView listView,
							int[] reverseSortedPositions) {
						for (int position : reverseSortedPositions) {
							SingleNote note = mListAdapter.getItem(position);
							mDbAdapter.open();
							mDbAdapter.dismissMessage(note.getId(), type);
							mDbAdapter.close();
							mListAdapter.removeAt(position);
						}
						mListAdapter.notifyDataSetChanged();
					}
				});
		mListNotes.setOnTouchListener(touchListener);
		// Setting this scroll listener is required to ensure that during
		// ListView scrolling,
		// we don't look for swipes.
		mListNotes.setOnScrollListener(touchListener.makeScrollListener());
	}

	private ArrayList<SingleNote> getAllNotes() {
		ArrayList<SingleNote> result = new ArrayList<SingleNote>();

		DbAdapter dbAdapter = new DbAdapter(this);
		dbAdapter.open();

		result = dbAdapter.getNotesForPopup(contactId, type);

		dbAdapter.close();
		return result;
	}

	private void doDismissAllNotes() {
		mDbAdapter.open();

		for (int i = 0; i < mListAdapter.getCount(); i++) {
			SingleNote note = mListAdapter.getItem(i);
			mDbAdapter.dismissMessage(note.getId(), type);
		}

		mDbAdapter.close();
	}

	public void log(String msg) {
		if (DBG) {
			Log.d(TAG, msg);
		}
	}
}
